﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ymk_camera_surveillance.Models;
using static ymk_camera_surveillance.Models.ObsCamera;

namespace ymk_camera_surveillance.Database.DBModels
{
    class DBCamera
    {
        //attrs
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [Indexed]
        public string Name { get; set; }
        [Indexed]
        public string RtspUrl { get; set; }
        public CameraStatus State { get; set; } = CameraStatus.DISCONNECT;
        //end attrs

        //conn
        private static readonly SQLiteConnection conn = DbInstance.GetConn();
        public DBCamera() { }


        public static List<DBCamera> Get(List<int> Id_s)
        {
            var table = conn.Table<DBCamera>();
            if (Id_s == null)
            {
                return table.Where(_ => true).OrderBy(c => c.Id).ToList();
            }

            return table.Where(c => Id_s.Contains(c.Id)).OrderBy(c => c.Id).ToList();
        }

        public static int Save(List<DBCamera> DBCamera_s)
        {
            var res = conn.InsertAll(DBCamera_s, runInTransaction: true);
            return res;
        }

        public static List<DBCamera> Update(List<int> Id_s)
        {
            return null;
        }


        public static int Delete(List<int> Id_s)
        {
            var table = conn.Table<DBCamera>();
            var res = table.Where(c => Id_s.Contains(c.Id)).Delete();
            return res;
        }

        // to Observable obj
        public static List<ObsCamera> ToObs(List<DBCamera> DBCamera_s)
        {
            var obsCameras = new List<ObsCamera>();
            foreach (var item in DBCamera_s)
            {
                obsCameras.Add(new ObsCamera { Id = item.Id, Name = item.Name, RtspUrl = item.RtspUrl, State = item.State });
            }
            return obsCameras;
        }

        public static List<DBCamera> FromObs(List<ObsCamera> ObsCamera_s)
        {
            var dbCameras = new List<DBCamera>();
            foreach (var item in ObsCamera_s)
            {
                dbCameras.Add(new DBCamera { Id = item.Id, Name = item.Name, RtspUrl = item.RtspUrl, State = item.State });
            }
            return dbCameras;
        }
    }
}
